/**
 * 
 */
package com.kth.subtranslator.ui.controller;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;

import javax.swing.BorderFactory;
import javax.swing.DefaultListCellRenderer;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JToolBar;

import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
import com.kth.subtranslator.ITranslateDriver;
import com.kth.subtranslator.TranslateEngine.Language;
import com.kth.subtranslator.evt.FileSelectedEvent;
import com.kth.subtranslator.evt.LanguageSelectedEvent;
import com.kth.subtranslator.evt.StopTranslateEvent;
import com.kth.subtranslator.ui.DriverRenderer;

/**
 * @author Kevin Thackray
 *
 */
public class MenuBarController {

	private JToolBar view;
	private Language selectedLanguage;
	private final JComboBox languageCombo;
	private final EventBus eventBus;
	private JComboBox driverCombo;
	private SrtPanelController srtController;

	/**
	 * @param eventBus 
	 * 
	 */
	public MenuBarController(final SrtPanelController srtController, final EventBus eventBus) {
		this.eventBus = eventBus;
		this.srtController = srtController;
		this.eventBus.register(this);
		view = new JToolBar("Subtitle");
		languageCombo = new JComboBox();
		languageCombo.setRenderer( new DefaultListCellRenderer() {

			@Override
			public Component getListCellRendererComponent(JList list, Object value,
					int index, boolean isSelected, boolean cellHasFocus) {
				JLabel l = (JLabel)super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
				if (index < 0) {
					l.setText("Subtitle Language : " + languageCombo.getSelectedItem());
				}
				return l;
			}
		});
		for (Language l :  srtController.getDefaultDriver().getSupportedLanguage()) {
			languageCombo.addItem(l);
			if (Language.ENGLISH.equals(l)) {
				languageCombo.setSelectedItem(l);
			}

		}

		languageCombo.addActionListener( new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				eventBus.post( new LanguageSelectedEvent() {

					@Override
					public Language getLanguage() {
						return (Language) getLanguageCombo().getSelectedItem();
					}
				});
			}
		});

		languageCombo.setEnabled(false);
		view.add(languageCombo);

		driverCombo = new JComboBox();
		driverCombo.setRenderer( new DriverRenderer() );
		for (ITranslateDriver d :  srtController.getDrivers()) {
			driverCombo.addItem(d);
		}
		driverCombo.setSelectedItem(srtController.getDefaultDriver());
		driverCombo.addActionListener( new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				languageCombo.removeAllItems();
				Collection<Language> supportedLanguages = ((ITranslateDriver)driverCombo.getSelectedItem()).getSupportedLanguage();
				for (Language l : supportedLanguages) {
					languageCombo.addItem(l);
					if (Language.ENGLISH.equals(l)) {
						languageCombo.setSelectedItem(l);
					}
				}
			}

		});
		driverCombo.setEnabled(false);
		view.add(driverCombo);

		JButton stopButton = new JButton();
		ImageIcon icon = new ImageIcon( getClass().getResource("stop.png"));
		
		stopButton.setIcon( icon );
		stopButton.setBorder(BorderFactory.createEmptyBorder());
		stopButton.addActionListener( new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				eventBus.post( new StopTranslateEvent() {});
			}
		});
		stopButton.setToolTipText("Stop translation");
		view.add(stopButton);
		
//		PlayButton playButton = new PlayButton();
//		playButton.setPreferredSize( new Dimension(30, 30));
//		view.add( playButton );
	}

	/**
	 * @return the view
	 */
	public JToolBar getView() {
		return view;
	}

	/**
	 * @return the languageCombo
	 */
	public JComboBox getLanguageCombo() {
		return languageCombo;
	}

	/**
	 * @return the driverCombo
	 */
	public JComboBox getDriverCombo() {
		return driverCombo;
	}

	public ITranslateDriver getSelectedDriver() {
		return (ITranslateDriver) getDriverCombo().getSelectedItem();
	}

	/**
	 * @return the selectedLanguage
	 */
	public Language getSelectedLanguage() {
		return (Language) getLanguageCombo().getSelectedItem();
	}


	@Subscribe
	public void onFileSelected(FileSelectedEvent event) {
		driverCombo.setEnabled(true);
		languageCombo.setEnabled(true);
	}

}
